//
//  GKNovelDirectoryView.swift
//  MySwiftObject
//
//  Created by wangws1990 on 2019/9/19.
//  Copyright © 2019 wangws1990. All rights reserved.
//

import UIKit
@objc protocol GKNovelDirectoryDelegate : NSObjectProtocol {
    @objc optional func closeDirectoryView(view :GKNovelDirectoryView);
    @objc optional func selectChapter(view :GKNovelDirectoryView, chapter:NSInteger);
}
class GKNovelDirectoryView: UIView,UITableViewDataSource,UITableViewDelegate {
    var chapter:GKNovelChapter!;
    var delegate : GKNovelDirectoryDelegate!;
    lazy var listData : [GKNovelChapter] = {
        return []
    }()
    lazy var tableView : UITableView = {
        let tableView : UITableView = UITableView.init(frame: CGRect.zero, style: .plain);
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.isUserInteractionEnabled = true;
        return tableView
    }()
    lazy var tapView : UIView = {
        let tapView : UIView = UIView.init();
        tapView.backgroundColor = UIColor.init(rgb: 0x000000, alpha: 0.3);
        return tapView;
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.loadUI();
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder:  aDecoder);
        self.loadUI();
    }
    func setDatas(listData:[GKNovelChapter]){
        self.listData.removeAll();
        self.listData.append(contentsOf: listData);
        self.tableView.reloadData();
    }
    func loadUI(){
        self.isUserInteractionEnabled = true;
        self.addSubview(self.tableView);
        self.addSubview(self.tapView);
        self.tableView.snp_makeConstraints { (make) in
            make.left.top.bottom.equalToSuperview();
            make.width.equalTo(SCREEN_WIDTH/5*4.0);
        }
        self.tapView.snp_makeConstraints { (make) in
            make.top.bottom.right.equalToSuperview();
            make.left.equalTo(self.tableView.snp_right);
        }
        let tap : UITapGestureRecognizer = UITapGestureRecognizer.init();
        self.tapView.addGestureRecognizer(tap);
        tap.addTarget(self, action: #selector(tapAction))
        
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1;
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.listData.count;
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 55;
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell  : GKNovelDirectoryCell = GKNovelDirectoryCell.cellForTableView(tableView: tableView, indexPath: indexPath)
        cell.selectionStyle = .none;
        let model : GKNovelChapter = self.listData[indexPath.row];
        if self.chapter != nil {
            cell.select = model.chapterId  == self.chapter.chapterId;
        }
        cell.imageLock.isHidden = !model.isVip;
        cell.titleLab.text = model.title ?? "";
        return cell;
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true);
        if self.delegate != nil {
            self.delegate.selectChapter!(view: self, chapter: indexPath.row);
        }
        self.tapAction();
    }
    @objc func tapAction(){
        if self.delegate != nil {
            self.delegate.closeDirectoryView?(view: self)
        }
    }
    
}
